// pages/route1/poster/poster.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    poster: {
      channelId: "xxxx",
      pic: "",
      qrcode: {
        errcode: 47001,
        errmsg: "data format error hint: [Nu0huA0528b451]"
      },
      subTitle: "VIP 区 399 元，普通区 299 元",
      title: "十里芳菲新生活美学周"
    },
    userinfo: null,
    options: null,
    image_bg: 'https://mini.server.tech84.com/img/2.jpg',
    user_title: "我已加入活动，邀请您一起参加",
    foot_title: '扫码了解活动详情',

    // subTitle1: "VIP 区",
    // subTitle2: "399",
    // subTitle3: '元, 普通区',
    // subTitle4: '299',
    // subTitle5:'元',
    winWidth: '',
    winHeight: '',
    userinfo: null,
    options: null,
    imageCode: 0,
    hidden: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  bindGetUserInfo: function (e) {
    console.log(e)
    console.log(wx.authorize({ scope: "scope.writePhotosAlbum" }))
  },
  onLoad: function (options) {
    console.log("xxxxxxxxxxxxxxxxx345xxxxx2")
    var that = this
    that.setData({
      options: options
    })
    console.log(options)
    wx.getUserInfo({
      success: res => {
        console.log(res.userInfo)
        that.setData({
          userinfo: res.userInfo
        })
      }
    })
    wx.request({
      url: app.globalData.rootUrl + '/sdk/share',
      method: "GET",
      data: {
        session: options.session,
        productId: options.productId
      },
      success: function (res) {
        res.data.data[0].price = []
        res.data.data[0].subTitle.forEach(
          function (item, index) {
            console.log(index)
            var array = item.split(",")
            res.data.data[0].price[index] = {
              tit1: array[0],
              tit2: array[1],
            }
          }
        )
        console.log(res.data.data[0])
        that.setData({
          poster: res.data.data[0]
        })
      }
    })
  },
  btn: function () {
    wx.showLoading({
      title: '处理中',
    })
    console.log("xx")
    var that = this;
    wx.getImageInfo({
      src: that.data.poster.pic,
      success: function (res) {
        var context = wx.createCanvasContext('canv');
        var user_photo_w = that.data.windowWidth / 750 * 22;
        var user_photo_h = that.data.windowWidth / 750 * 7;
        var user_name_w = that.data.windowWidth / 750 * 0;
        var user_name_h = that.data.windowWidth / 750 * 0;
        var user_title_w = that.data.windowWidth / 750 * 0;
        var user_title_h = that.data.windowWidth / 750 * 0;
        // 
        var content_text_w = that.data.windowWidth / 750 * 0;
        var content_text_h = that.data.windowWidth / 750 * 0;
        //
        var foot_title_w = that.data.winWidth / 750 * 0;
        var foot_title_h = that.data.winWidth / 750 * 0;

        var foot_title1_w = that.data.winWidth / 750 * 0;
        var foot_title1_h = that.data.winWidth / 750 * 0;
        //
        var qrimg_w = that.data.windowWidth / 750 * 270;
        var qrimg_h = that.data.windowWidth / 750 * 496;
        //背景图
        context.drawImage(res.path, 0, 0, 750, 1146);
        //头像
        wx.getImageInfo({
          src: that.data.userinfo.avatarUrl,
          success: function (res) {
            context.save();
            context.arc(98, 80, 60, 0, 2 * Math.PI);
            context.clip();
            context.drawImage(res.path, 30, 20, 130, 130);
            context.restore()
            context.setFontSize(32);
            context.setFillStyle('#2b63e9');
            context.fillText(that.data.userinfo.nickName, 170, 60);
            context.setFontSize(24);
            context.fillText(that.data.user_title, 170, 100);
            context.setFontSize(50);
            context.restore()
            wx.getImageInfo({
              src: 'https://' + that.data.poster.qrcode,
              success: res => {
                context.drawImage(res.path, 538, 950, 190, 190)
                context.draw(false, function () {
                  wx.canvasToTempFilePath({
                    canvasId: 'canv',
                    success: (res) => {
                      var shareTempFilePath = res.tempFilePath;
                      wx.saveImageToPhotosAlbum({
                        filePath: shareTempFilePath,
                        success: function (res) {
                          wx.showToast({
                            title: '已保存到相册'
                          })
                        },
                        fail: function (err) {
                          console.log(err)
                        },
                        complete: function () {
                          wx.hideLoading()
                        },
                      })

                    },
                    fail: function (res) {
                      console.log(res)
                    }
                  })
                })

              }
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('sss')
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '我已加入活动，邀请您一起参加',
      path: 'pages/index/index?productId=' + this.data.options.productId + '&refId=' + wx.getStorageSync('memberId')
    }
  }
})